<?php	
require_once('config/lang/eng.php');
require_once('tcpdf.php');
include 'connection.php';

$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

list ($start_month, $start_day, $start_year) = split('/', $start_date);
list ($end_month, $end_day, $end_year) = split('/', $end_date);

$startdate = mktime(0, 0, 0, $start_month, $start_day, $start_year);
$enddate = mktime(0, 0, 0, $end_month, $end_day, $end_year); 

//echo $startdate;
//echo "<br />";
//echo $enddate;


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Tech Head Solutions');
$pdf->SetTitle('Jobsheets');
$pdf->SetSubject('Jobsheets');
$pdf->SetKeywords('');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.'', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', 10);

// add a page
$pdf->AddPage();

/* NOTE:
 * *********************************************************
 * You can load external XHTML using :
 *
 * $html = file_get_contents('/path/to/your/file.html');
 *
 * External CSS files will be automatically loaded.
 * Sometimes you need to fix the path of the external CSS.
 * *********************************************************
 */

// define some HTML content with style
$pdf->SetFont('helvetica', 11);
$html = <<<EOF
<!-- EXAMPLE OF CSS STYLE -->
<style>
.row0 {
    background-color: #ffffff;
}

.row1 {
    background-color: #f0f0f0;
}
</style>
<div class="test">
<img src="images/logo.jpg" border="0" />
<br />
<br />
<table border="1" cellspacing="0" cellpadding="2">
<tr><td width= "100px"><b>Name</b></td><td width= "80px"><b>Date</b></td><td width= "50px"><b>Signed (Y/N)</b></td><td width= "40px"><b>Paid (Y/N)</b></td><td width="50px"><b>Hours</b></td><td width= "60px"><b>Tech Head</b></td><td width= "200px"><b>Hardware/Software</b></td><td width= "200px"><b>Job Description</b></td><td width= "200px"><b>Outstanding / Questions</b></td></tr>
EOF;

$query = "SELECT c.name, t.done_date, t.is_signed_for, t.is_paid, t.hours, u.username, t.hardware_software, t.description, t.outstanding_questions from pp086_companies c, pp086_users u, pp086_project_times t, pp086_project_companies p where t.assigned_to_user_id = u.id and t.project_id = p.project_id and p.company_id = c.id and t.done_date BETWEEN FROM_UNIXTIME(".$startdate.") and FROM_UNIXTIME(".$enddate.") order by c.name, t.done_date";

$results = mysql_query($query);
$rowclass = 1;

while($row = mysql_fetch_row($results))
  {
	  $name = $row['0'];
	  $date = date('D d F', strtotime($row['1']));
	  if ($row['2'] == 1){
	  $signed = "Yes";}
	  else
	  {$signed = "No";}
	  if ($row['3'] == 1){
	  $paid = "Yes";}
	  else
	  {$paid = "No";}
	  $hours = $row['4'];
	  $username = $row['5'];
	  $hardwaresoftware = $row['6'];
	  $description = $row['7'];
	  $outstandingquestions = $row['8'];
	  $class = "row".$rowclass;
	  echo $class;
	
if ($class == "row1"){  
$html .= "<tr class=\"row1\"><td>$name</td><td>$date</td><td><center>$signed</center></td><td><center>$paid</center></td><td><center>$hours</center></td><td>$username</td><td>$hardwaresoftware</td><td>$description</td><td>$outstandingquestions</td></tr>";
}
if ($class == "row0"){
$html .= "<tr class=\"row0\"><td>$name</td><td>$date</td><td><center>$signed</center></td><td><center>$paid</center></td><td><center>$hours</center></td><td>$username</td><td>$hardwaresoftware</td><td>$description</td><td>$outstandingquestions</td></tr>";
}
$rowclass = 1 - $rowclass;
}
//var_dump(get_defined_vars());
$html .= "</table>";

// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// *******************************************************************
// HTML TIPS & TRICKS
// *******************************************************************

// REMOVE CELL PADDING
//
// $pdf->SetCellPadding(0);
// 
// This is used to remove any additional vertical space inside a 
// single cell of text.

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// REMOVE TAG TOP AND BOTTOM MARGINS
//
// $tagvs = array('p' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0)));
// $pdf->setHtmlVSpace($tagvs);
// 
// Since the CSS margin command is not yet implemented on TCPDF, you
// need to set the spacing of block tags using the following method.

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// SET LINE HEIGHT
//
// $pdf->setCellHeightRatio(1.25);
// 
// You can use the following method to fine tune the line height
// (the number is a percentage relative to font height).

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// CHANGE THE PIXEL CONVERSION RATIO
//
// $pdf->setImageScale(0.47);
// 
// This is used to adjust the conversion ratio between pixels and 
// document units. Increase the value to get smaller objects.
// Since you are using pixel unit, this method is important to set the
// right zoom factor.
// 
// Suppose that you want to print a web page larger 1024 pixels to 
// fill all the available page width.
// An A4 page is larger 210mm equivalent to 8.268 inches, if you 
// subtract 13mm (0.512") of margins for each side, the remaining 
// space is 184mm (7.244 inches).
// The default resolution for a PDF document is 300 DPI (dots per 
// inch), so you have 7.244 * 300 = 2173.2 dots (this is the maximum 
// number of points you can print at 300 DPI for the given width).
// The conversion ratio is approximatively 1024 / 2173.2 = 0.47 px/dots
// If the web page is larger 1280 pixels, on the same A4 page the 
// conversion ratio to use is 1280 / 2173.2 = 0.59 pixels/dots

// *******************************************************************

// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$filename = $_POST['file_name'].'.pdf';
$pdf->Output($filename, 'F');

?>
<script type="text/javascript">
	var filename = "<?php echo $filename; ?>";
	window.open (filename)</script>
<?php

//Offer download link
//echo "To download your warranty certificate, <a href='warranty_$lastname.pdf'>click here</a>";

//function emailPdf ($filename){ 

//$sendfilename = $filename;
//Create the Transport
//$transport = Swift_MailTransport::newInstance();

//Create the Mailer using your created Transport
//$mailer = Swift_Mailer::newInstance($transport);

//Create the Transport
//$transport = Swift_SmtpTransport::newInstance('mail.bigpond.com.au', 25);

//Create the Mailer using your created Transport
//$mailer = Swift_Mailer::newInstance($transport);

//Create the message
//$message = Swift_Message::newInstance()

  //Give the message a subject
  //->setSubject('Warranty Registration')

  //Set the From address with an associative array
  //->setFrom(array('info@techheadsolutions.com.au' => 'Tech Head Solutions'))

  //Set the To addresses with an associative array
  //->setTo(array('kevin.muchai@gmail.com' => 'Kevin Muchai'))

  //Give it a body
  //->setBody('Attached is your warranty certificate. Great stuff.')

  //Optionally add any attachments
  //->attach(Swift_Attachment::fromPath('$sendfilename'))
  //;
  
  //Send the message
	//$numSent = $mailer->send($message);

	//printf("Sent %d messages\n", $numSent);
	
//}

//============================================================+
// END OF FILE                                                
//============================================================+
